package com.offcn.bigdata.spark.p1.p3

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

import scala.reflect.ClassTag

/**
  * 排序的action操作：
  *     如果我们要获取排序之后的rdd数据，官方建议我只操作takeOrder的操作
  *     而不是先sortBy或者sortByKey，然后在collect
  */
object _08TakeOrderOps {
    def main(args: Array[String]): Unit = {
        val conf = new SparkConf()
            .setMaster("local[*]")
            .setAppName(s"${_08TakeOrderOps.getClass.getSimpleName}")
        val sc = new SparkContext(conf)

        val list = List(
            Student(1, "林博", 18, 180),
            Student(2, "单松", 19, 150),
            Student(3, "张皓", 20, 120),
            Student(4, "王建", 20, 119),
            Student(106, "冯岩", 30, 10086)
        )
        val rdd = sc.parallelize(list)

        //现在要对rdd进行按照年龄排序，然后获取其中的3条记录 TopN
        val ret = rdd.takeOrdered(2)(new Ordering[Student](){
            override def compare(x: Student, y: Student): Int = y.height.compareTo(x.height)
        })

        ret.foreach(println)


        sc.stop()
    }

    case class Student(id: Int, name: String, age: Int, height: Double)
}
